/*
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.enseirb.mycub.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;

import fr.enseirb.mycub.R;
import fr.enseirb.mycub.activity.AbstractPreferencesActivity;
import fr.enseirb.mycub.activity.MainActivity;

/**
 * A class that handles some common activity-related functionality in the app,
 * such as setting up the action bar. This class provides functionality useful
 * for both phones and tablets, and does not require any Android 3.0-specific
 * features.
 */
public class ActivityHelper {
	protected Activity mActivity;
	private ProgressDialog mProgressDialog = null;
	public static final long MIN_UPDATE_TIME = 3 * 1000 * 60; // 3minutes de temps entre les refresh
	private String mMessage = null;
	public static Handler mHandler;

	public ActivityHelper(Activity activity) {
		mActivity = activity;
		if(mHandler == null )mHandler = new Handler();
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setCancelable(false).setPositiveButton(R.string.Ok,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
	}

	/**
	 * Invoke "home" action, returning to
	 * {@link com.google.android.apps.iosched.ui.HomeActivity}.
	 */
	public Intent getHomeIntent() {
		final Intent intent = new Intent(mActivity, MainActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		return intent;
	}

	/**
	 * Invoke "search" action, triggering a default search.
	 */
	public void goSearch() {
		mActivity.startSearch(null, false, Bundle.EMPTY, false);
	}
	public Dialog onCreateDialog(int id) {
		if (id == R.id.json_error || id == R.id.database_error
				|| id == R.id.network_error || id == R.id.message) {
			
			Log.i("Activity helper", "Error id : "+id);
			return new AlertDialog.Builder(mActivity).setCancelable(false)
					.setPositiveButton(R.string.Ok, new OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					}).setTitle(R.string.network_error).setMessage(
							R.string.network_error_summary).create();
			// setTitle() and setMessage() are used because if not dialog will
			// be unable to set correct
			// resources in onPrepareDialog.
		} else if (id == R.id.no_location_provider || id == R.id.enable_gps) {
			return new AlertDialog.Builder(mActivity).setCancelable(false)
					.setPositiveButton(R.string.yes, new OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int id) {
							Intent gpsOptionsIntent = new Intent(
									android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
							mActivity.startActivity(gpsOptionsIntent);
						}
					}).setNegativeButton(R.string.no, new OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					}).setTitle(R.string.gps_disabled).setMessage(
							R.string.location_disabled).create();
		} else if (id == R.id.stations_update) {
			mProgressDialog = new ProgressDialog(mActivity);
			return mProgressDialog;
		} else {
			return null;
		}
	}

	public void onPrepareDialog(int id, Dialog dialog) {
		switch (id) {
		case R.id.json_error:
			((AlertDialog) dialog).setTitle(R.string.json_error);
			((AlertDialog) dialog).setMessage(mActivity
					.getString(R.string.json_error_summary));
			break;
		case R.id.database_error:
			((AlertDialog) dialog).setTitle(R.string.db_error);
			((AlertDialog) dialog).setMessage(mActivity
					.getString(R.string.db_error_summary));
			break;
		case R.id.network_error:
			((AlertDialog) dialog).setTitle(R.string.network_error);
			((AlertDialog) dialog).setMessage(mActivity
					.getString(R.string.network_error_summary));
			break;
		case R.id.no_location_provider:
			((AlertDialog) dialog).setTitle(R.string.location_disabled);
			((AlertDialog) dialog).setMessage(mActivity
					.getString(R.string.should_enable_location));
			break;
		case R.id.enable_gps:
			((AlertDialog) dialog).setTitle(R.string.gps_disabled);
			((AlertDialog) dialog).setMessage(mActivity
					.getString(R.string.should_enable_gps));
			break;
		case R.id.stations_update:
			((ProgressDialog) dialog).setMessage(mActivity
					.getString(R.string.stations_update));
			((ProgressDialog) dialog).setCancelable(false);
			break;
		case R.id.message:
			((AlertDialog) dialog).setTitle(mActivity
					.getString(R.string.important));
			((AlertDialog) dialog).setMessage(mMessage);
		}
	}

	public void onReceiveResult(int resultCode, Bundle resultData) {
//		if (resultCode == SyncService.STATUS_UPDATE_STATIONS) {
//			mActivity.showDialog(R.id.stations_update);
//		} else if (resultCode == SyncService.STATUS_UPDATE_STATIONS_FINISHED
//				|| resultCode == SyncService.STATUS_SYNC_STATIONS_FINISHED) {
//			if (resultCode == SyncService.STATUS_UPDATE_STATIONS_FINISHED) {
//				try{
//				mActivity.dismissDialog(R.id.stations_update);
//				}
//				catch(IllegalArgumentException e){
//					
//				}
//			}
//			String message = resultData.getString(SyncService.EXTRA_RESULT);
//			if (message != null) {
//				mMessage = message;
//				mActivity.showDialog(R.id.message);
//			}
//		}
	}
	
}
